package pri.ydy.eduorder.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import pri.ydy.commonUtils.ordervo.CourseInfoVoOrder;
import pri.ydy.commonUtils.ordervo.UcenterMemberOrder;
import pri.ydy.eduorder.client.EduClient;
import pri.ydy.eduorder.client.UcenterClient;
import pri.ydy.eduorder.entity.Order;
import pri.ydy.eduorder.mapper.OrderMapper;
import pri.ydy.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pri.ydy.eduorder.utils.OrderNoUtil;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author YeDongyong
 * @since 2021-02-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //1.生成订单，需要用户信息和课程信息
    @Override
    public String createOrder(String courseId, String memberId) {

        System.out.println(courseId);
        System.out.println(memberId);
        //2.根据课程id通过远程调用获得课程信息
        CourseInfoVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        System.out.println(courseInfoOrder.getCover());
        System.out.println(courseInfoOrder.getTeacherName());
        //1.根据用户id通过远程调用获得用户信息
        System.out.println("33333333333333333333333");
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        System.out.println("55555555555555555555555");



        //创建order对象,向order对象设置需要的数据
        Order order=new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//特定规则随机订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());

        order.setStatus(0);//支付状态 (0未支付,1已支付)
        order.setPayType(1);//支付类型,微信1
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
